home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2027 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  74 lines

  1. Path: news.infi.net!usenet
  2. From: nngis@norfolk.infi.net (Greg DiGiorgio)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Overwriting file information?
  5. Date: 18 Jan 1996 14:37:21 GMT
  6. Organization: Customer of InfiNet
  7. Message-ID: <4dllv1$7af@news.infi.net>
  8. References: <Ed-1601960129180001@allentown03.voicenet.com>
  9. Reply-To: nngis@norfolk.infi.net
  10. NNTP-Posting-Host: h-hengest.norfolk.infi.net
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.99.3
  13.  
  14. In article <Ed-1601960129180001@allentown03.voicenet.com>, Ed@eSquare.com 
  15. says...
  16. >
  17. >I need to write some code that will open an existing file, find a string 
  18. and then update the string's counter. Nothing here is a problem except 
  19. that I don
  20. >
  21. >Thanks & if anyone reading this is wondering how I am doing the rest of 
  22. the stuff, just throw some questions at me & I'd be happy to help a 
  23. fellow newbie.
  24. >
  25. >Ed.
  26. >
  27. >---------
  28. >example:
  29. >---------
  30. >Changing a file containing:
  31. >
  32. >duck 15
  33. >fred 96
  34. >bob 342
  35. >
  36. >to:
  37. >
  38. >duck 15
  39. >fred 97
  40. >bob 342
  41.  
  42. /*                      doit.c
  43.  * When you run this program it prints out each line from
  44.  * the file to "stdout" which you can then redirect to
  45.  * another file to test the changes you have made.
  46.  * This program will work in MSDOS and UNIX as is. It does not
  47.  * read/write the orig file which you could do with random file
  48.  * I/O - which this example does not use.
  49.  *
  50.  * Run from the cmd line like: doit infilename
  51.  */
  52. #include <stdio.h>
  53. int main(int argc, char *argv[]) {
  54.     FILE *f;
  55.     char buf[256];
  56.  
  57.     if (argc!=2) return(1);
  58.     f=fopen(argv[1],"rt");
  59.     if (!f) {
  60.         printf("Sorry, can't open %s\n",argv[1]);
  61.         return(1);
  62.     }
  63.     while (fgets(buf,255,f)) {
  64.         if (!strcmp(buf,"fred 96\n")) 
  65.             strcpy(buf,"fred 97\n");
  66.         printf("%s",buf);    
  67.     }
  68.     fclose(f);
  69. }
  70.  
  71. Hope this helps,
  72. Greg DiGiorgio
  73.  
  74.